import api from "../../../../../common/api";
import { getVip } from "../../../../../models/user";
import { HOUSE_OWER_PHONE } from "../../../../../common/menuId";
import { menuHandler } from "../../../../../common/menuHandler.js";
import { isReleaseUrl } from "../../../../../utils/util"
// pages/home/propertyOwner/houseList/houseList.js

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isVip: {
      type: Boolean,
      default: false
    },

    house: {
      type: Array,
      value: [],
    },
  },
  //监听组件数据
  observers: {
    house(){
      let house = [...this.data.house]
      for(var i in house){
        house[i].price= `${house[i].price/10000}万`
        if(house[i].wei==='0'){
          house[i].door= `${house[i].shi}室/${house[i].ting}厅`
        } else {
          house[i].door= `${house[i].shi}室/${house[i].ting}厅/${house[i].wei}卫`
        }
        
        house[i].buildingSize = `${house[i].buildingSize}平`
        if(house[i].saleType==='SALES'){  //二手
          house[i].unitPriceStr = `${house[i].unitPriceStr}元/平`
        } else {
          if(house[i].unitPriceStr && house[i].unitPriceStr.indexOf('元/月')>-1){
            house[i].unitPriceStr = house[i].unitPriceStr
          } else {
            house[i].unitPriceStr = `${house[i].unitPriceStr}元/月`
          }
          
        }
        
      }
      this.setData({
        _house: house
      })
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    showGuide: false,
    curId: '', //当前的房源id
    _house: []
  },
  /**
   * 组件的生命周期
   */
  lifetimes: {
    attached: function () {
    },
    
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    _cancelEvent(e) {
      let id = e.currentTarget.dataset.id; //传递给父组件的参数
      this.triggerEvent("cancelEvent", id);
    },
    _cancelEventX(e) { 
      let id = e.currentTarget.dataset.id; //传递给父组件的参数
      this.triggerEvent("cancelEventX", id);
    },
    _confirmEvent(e) {
      this.triggerEvent("confirmEvent");
    },

    _setHouseInfo() {
      const sessionUser = getApp().globalData.sessionUser;
      const wechatOpenid = sessionUser.extProps.wechatOpenid;
      const agentid = sessionUser.id;
      const houseItem = this.data.house.find((v) => v.id === this.data.curId)

      const url = `https://api${isReleaseUrl ? '' : '-dev'}.shulanfd.com/shulan-jjr-gateway/gatherHouse/index.html?url=${houseItem.externalInfoUrl}&agentId=${agentid}&id=${houseItem.id}`

      console.log("####请求保存 58房源####", url);
      
      let message = {
        touser: wechatOpenid,
        msgtype: "link",
        link: {
          title: `来源：${houseItem.externalName}业主房源`,
          description: houseItem.title,
          url,
          thumb_url: houseItem.coverPicUrl || "/images/defaultImgSmall.jpg",
        },
      };

      api.oldHouseInfo({
        agentid,
        openid: wechatOpenid
      }, () => {
        api.setHouseInfo(
          {
            agentid,
            openid: wechatOpenid,
            message: JSON.stringify(message),
          },
          (res) => {}
        );
      })
    },

    handleOnConcat() {
      this.setData({ showGuide: false })
      this._setHouseInfo()
      
      // 使用次数
      api.getMemberPower({ menuId: HOUSE_OWER_PHONE });
    },

    enterConversation(e) {
      this.setData({
        curId: e.currentTarget.dataset.id
      })

      if (!this.data.isVip) {
        menuHandler.call(this, HOUSE_OWER_PHONE).then(() => {
          this.setData({ showGuide: true })
        })
        .catch(() => {})
        
      } else {
        this._setHouseInfo()
      }
    }
  },
});
